package com.itfreer.ui.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.itfreer.ui.base.api.IPower;
import com.itfreer.utils.common.StringUtils;
import com.itfreer.utils.config.ConfigManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义itfreer基础Activity控制器
 * 解决返回退出提示，HOME控制，旋转控制
 */
public class BaseActivity extends AppCompatActivity {

    private List<String> securitys=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE（横屏） 或 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT（竖屏）
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }

    /**
     * 返回时，是否可以退出
     * @return
     */
    protected boolean canExit(){
        return false;
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(canExit()){
            if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
                if((System.currentTimeMillis()-exitTime) > 2000){
                    Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    finish();
                    System.exit(0);
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    protected View getRootView(Activity context) {
        return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
    }

    /**
     * 获取所有的子控件
     * @param view
     * @return
     */
    protected List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    /**
     * 获取权限ID集
     */
    protected List<String> getSecuritys(){
        if(securitys==null){
            securitys = (List<String>) ConfigManager.getConfig(IPower.DataLabel);
            if(securitys==null){
                securitys = new ArrayList<>();
            }
        }
        return securitys;
    }

    @Override
    protected void onStart() {
        super.onStart();
        initSecuritys();
    }

    /**
     * 获取数据
     */
    protected void initSecuritys(){
        List<String> securitys= getSecuritys();
        if(securitys==null || securitys.size()<=0){
            return;
        }
        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);
        for(View item : viewList){
            if(item instanceof IPower){
                IPower security = (IPower)item;
                if(!StringUtils.IsNullOrSpace(security.getSecurityID())){
                    if(securitys.contains(security.getSecurityID())){
                        security.setSecurityVisible(true);
                    }else{
                        security.setSecurityVisible(false);
                    }
                }
            }
        }
    }
}
